package com.ruicar.afs.cloud.admin.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.admin.dto.holiday.Month;
import com.ruicar.afs.cloud.admin.entity.SysHolidayInfo;
import com.ruicar.afs.cloud.admin.service.SysHolidayInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * The type SysHolidayInfoController
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.08.05 16:04:52
 * @since 1.0
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/holiday")
public class SysHolidayInfoController {
    /**
     * Sys holiday info service
     * <p>Description: </p>
     */
    private final SysHolidayInfoService sysHolidayInfoService;

    /**
     * List all holiday info response
     * 查询已维护节假日，前后当前时间10年
     * @return the response
     */
    @GetMapping("/listHoliday")
    public IResponse<List<SysHolidayInfo>> listAllHolidayInfo(){
        int year = DateUtil.year(DateUtil.date());
        return  IResponse.success(sysHolidayInfoService.list(Wrappers.<SysHolidayInfo>lambdaQuery().between(SysHolidayInfo::getYear,String.valueOf(year-10),String.valueOf(year+10)).orderByAsc(SysHolidayInfo::getYear)));
    }
    @GetMapping("/{year}")
    public IResponse<List<Month>> getYearDays(@PathVariable String year){
        return IResponse.success(sysHolidayInfoService.getYearHoliday(year));
    }
    @PostMapping("/saveHoliday")
    public IResponse<Boolean> saveHoliday(@RequestParam String year,@RequestParam String holidayInfo){
        if( sysHolidayInfoService.count(Wrappers.<SysHolidayInfo>lambdaQuery().eq(SysHolidayInfo::getYear,year))==0){
            SysHolidayInfo sysHolidayInfo = new SysHolidayInfo();
            sysHolidayInfo.setYear(year);
            sysHolidayInfo.setHolidayInfo(holidayInfo);
            sysHolidayInfoService.save(sysHolidayInfo);
        }else{
            sysHolidayInfoService.update(Wrappers.<SysHolidayInfo>lambdaUpdate().set(SysHolidayInfo::getHolidayInfo,holidayInfo).eq(SysHolidayInfo::getYear,year));
        }

        return IResponse.success(Boolean.TRUE);
    }

    @GetMapping("/listHolidayByYear")
    public IResponse<Map<String,String>> listHolidayByYear(@RequestParam("year") int year){
        Map<String,String> rtn = sysHolidayInfoService.list(Wrappers.<SysHolidayInfo>lambdaQuery().between(SysHolidayInfo::getYear,String.valueOf(year-5),String.valueOf(year+10)))
                .stream().collect(Collectors.toMap(SysHolidayInfo::getYear,SysHolidayInfo::getHolidayInfo));
        return  IResponse.success(rtn);
    }
}
